package io.renren.controller;

import io.renren.common.utils.HttpContextUtils;
import io.renren.common.utils.IPUtils;
import io.renren.common.utils.R;
import io.renren.entity.SysLogEntity;
import io.renren.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * 
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2019-01-20 12:55:41
 */
@RestController
@RequestMapping("api/log")
public class UserLogController {
    @Autowired
    private SysLogService sysLogService;


    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody SysLogEntity sysLog){
        //获取request
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();

        //设置IP地址
        sysLog.setIp(IPUtils.getIpAddr(request));

        //从header中获取userId
        String username = request.getHeader("userId");
        sysLog.setUsername(username);

        sysLog.setTime(System.currentTimeMillis());
        sysLog.setCreateDate(new Date());
        //保存系统日志
        sysLogService.insert(sysLog);

        return R.ok();
    }

}
